using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GuideManager : MonoBehaviour
{
    public GameObject guide;
    public GameObject shou;
    public GameObject zhuhe;
    private int step = 1;

    private Goods targetGoods;
    private GameObject targetContainer;

    public static GuideManager Instance { get; private set; }

    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }

        guide.SetActive(false);
        zhuhe.SetActive(false);
        shou.SetActive(false);
    }

    public void ShowGuide()
    {
        guide.SetActive(true);
        List<Container> containerList = Data.levelsTransform.GetComponent<Levels>().containers;
        for (int i = 0; i < containerList.Count; i++)
        {
            Container container = containerList[i];
            container.transform.SetParent(guide.transform.Find("Mask"));
            Goods[] goodsList = container.frontObject.GetComponentsInChildren<Goods>();
            if (goodsList.Length == 1)
            {
                shou.SetActive(true);
                Vector3 tpos = goodsList[0].transform.position;
                Vector3 newPosition = new Vector3(tpos.x + 55, tpos.y - 130, tpos.z);
                shou.transform.SetPositionAndRotation(newPosition, Quaternion.identity);
                shou.transform.SetAsLastSibling();
                targetGoods = goodsList[0];
            }
            if (goodsList.Length == 2)
            {
                for (int j = 0; j < container.frontObject.transform.childCount; j++)
                {
                    Transform child = container.frontObject.transform.GetChild(j);
                    Transform empty = child.Find("Goods");
                    if (empty == null)
                    {
                        targetContainer = child.gameObject;
                    }
                }
            }
        }
    }

    public void OnClick()
    {
        if (step == 1)
        {
            step++;
            targetGoods.OnClickGoods();
            Vector3 tpos = targetContainer.transform.position;
            Vector3 newPosition = new Vector3(tpos.x + 60, tpos.y - 120, tpos.z);
            shou.transform.SetPositionAndRotation(newPosition, Quaternion.identity);
            return;
        }
        if (step == 2)
        {
            step++;
            targetContainer.GetComponent<FrontClick>().MoniClick(targetContainer);
        }
    }

    public void HideGuide()
    {
        guide.SetActive(false);
    }

    public IEnumerator Zhuhe()
    {
        zhuhe.SetActive(true);
        shou.SetActive(false);
        PlayerPrefs.SetInt("Guide", 1);
        yield return new WaitForSeconds(1f);
        HideGuide();
        PanelManager.Instance.OpenPanel<PassPanel>();
    }
}
